﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iTextSharp.text.pdf;

namespace ModifyPDFTemplate.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            BaseFont.AddToResourceSearch("iTextAsian-1.0.dll");
              BaseFont.AddToResourceSearch("iTextAsianCmaps-1.0.dll");
              //BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);


           // BaseFont font = BaseFont.CreateFont("c:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
              //BaseFont font = BaseFont.CreateFont("c:/windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
           BaseFont font = BaseFont.CreateFont("c:/windows/fonts/李国夫手写字体.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
           //   BaseFont font = BaseFont.CreateFont("c:/windows/fonts/方正静蕾简体.ttf", BaseFont.IDENTITY_H, false);

            

            var path = Server.MapPath("~");

            ///string fileNameExisting = path + "\\template\\ttt.pdf";
            string fileNameExisting = path + "\\template\\newt.pdf";
            //string fileNameExisting = path + "\\template\\order_template.pdf";
           // @"C:\path\to\existing.pdf";
            string fileNameNew = path+ "\\download\\new.pdf";

            using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
            using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
            {
                // Open existing PDF
                var pdfReader = new PdfReader(existingFileStream);

                // PdfStamper, which will create
                var stamper = new PdfStamper(pdfReader, newFileStream);

                var form = stamper.AcroFields;
                var fieldKeys = form.Fields.Keys;

                foreach (string fieldKey in fieldKeys)
                {
                    form.SetFieldProperty(fieldKey, "textfont", font, null);
                    form.SetField(fieldKey, "REPLACED!中国人erere温馨指示：请仔细阅读宝贝详情及本店公告。拍下商品即视为同意本店所有条款。代购有风险，请亲考虑仔细。 精简事项：全是实在话，只求真心换真心，谢谢合作！ 美国现货通常在拍下付款后一周左右发货，使用包关税物流。美国现货在美国拼单统一运回中国MOMO库房，再分发寄出。美国现货及代购商品需至少2-3周左右时间到手（主要是要取决中国海关审查速度），急者勿拍；如有特殊时间要求的请拍前与店主联系，以免耽误您的事情和不必要的事后纠结。亲如不参加拼单，想付款后马上发货或想从美国直邮到家，请与我方联系补邮费差价即可。 ");


                }

                // "Flatten" the form so it wont be editable/usable anymore
                stamper.FormFlattening = true;

                // You can also specify fields to be flattened, which
                // leaves the rest of the form still be editable/usable
                stamper.PartialFormFlattening("field1");

                stamper.Close();
                pdfReader.Close();
            }

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
